package com.zd.commodity.supplierMng.action;

import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.zd.commodity.action.BaseAction;
import com.zd.commodity.brandMng.entity.BrandShow;
import com.zd.commodity.supplierMng.biz.ISupplierBiz;
import com.zd.commodity.supplierMng.dao.ISupplierDao;
import com.zd.commodity.supplierMng.entity.Supplier;
@Scope("prototype")
@Controller("SupplierAction")
public class SupplierAction extends BaseAction<Supplier> {
	private ISupplierBiz supplierBiz;

	public ISupplierBiz getSupplierBiz() {
		return supplierBiz;
	}
	@Autowired
	public void setSupplierBiz(ISupplierBiz supplierBiz) {
		this.supplierBiz = supplierBiz;
		this.setBaseBiz(supplierBiz);
	}
	/**
	 * 数据导出
	 */
	public void excelExport() throws Exception{
		Map<String, String> map = new HashMap<String, String>();
		Supplier supplier=super.getT1();
		map.put("name", supplier.getName());
		
		List list = supplierBiz.getExcelExportListBySql(BrandShow.class,"brandShowExcelExport",map.size()>0?map:null, -1, -1);
		
		String[] title = new String[]{"供应商名称","创建时间","创建人"};
		String[] neededFields = new String[]{"name","creationtime","creator"};
		super.excelExport("供应商管理", list,title,neededFields);
	}
	/**
	 * 下载模板
	 * @throws Exception 
	 */
	public void downloadTemplet() throws Exception{
		List brandTemplet = supplierBiz.getExcelExportListBySql(BrandShow.class, "downloadTemplet", null, 0, 0);
		String[] title = new String[]{"供应商名称"};
		String[] neededFields = new String[]{"name","code"};
		super.excelExport("供应商上传模板", brandTemplet,title,neededFields);
	}
	/**
	 * 模板导入
	 * @throws Exception 
	 */
	public void uploadExeclData() throws Exception{
		String message  = "上传成功";
		HashMap<Integer, String> map = new HashMap<Integer ,String>();
		//map.put(0, "notNull");
		map.put(0, "queryRepeat|supplier|name");
		List<String[]> data = supplierBiz.readExecl(map);
		if(data.size()==1){
			String[] strings = data.get(0);
			if("0".equals(strings[0])){
				message = strings[1];
			}
		}
		if(message.equals("上传成功")){
			for (String[] strings : data) {
				Supplier supplier = new Supplier();
				supplier.setName(strings[0]);
				supplierBiz.add(supplier);
			}
		}
		response.setHeader("Cache-Control", "no-cache");
		response.setContentType("text/html;charset=UTF-8");
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		
		out.write(message);
		out.flush();
		out.close();
	}
	
}
